/*********************************************************************
* File: AgentAndWorldSampleAgent.cpp
*
* Implements the combat and finances API sample. 
*
* \author Instituto Superior Tecnico de Lisboa, Portugal
* \author Technical University of Lisbon, Portugal
* 
* \author Agentes Autonomos e Sistemas Multiagente
* \author Autonomous Agents and Multiagent Systems
* 
* \version	1.0
* \date		2006/2007
*********************************************************************/
#include "AgentAndWorldSampleAgent.h"
using namespace agents::samples;

#define TIMEOUT_NOT_DEFINED -1

#include "../../util/debug.h"
#include "../../util/util.h"
using namespace util;

AgentAndWorldSampleAgent::AgentAndWorldSampleAgent(edict_t *enity, int team, int model)
:Agent(enity, team, model)
{}

AgentAndWorldSampleAgent::~AgentAndWorldSampleAgent(){}

void AgentAndWorldSampleAgent::newRound()
{
	_isMonitoringDamage = false;
	Agent::newRound();
}

void AgentAndWorldSampleAgent::think()
{
	if(_isMonitoringDamage && hasTakenDamage())
	{
		getCommunication().sayText("I'm under attack!\n");
		return;
	}

	ICommunication::MessageRecord record;
	if(getCommunication().receiveMsg(ICommunication::SayMessages::sayMsg, &record))
		handleMessage(record);
	else if(getCommunication().receiveMsg(ICommunication::SayMessages::sayTeamMsg, &record))
		handleMessage(record);
	else if(getCommunication().receiveMsg(ICommunication::SayMessages::sayDeadMsg, &record))
		handleMessage(record);
}
void AgentAndWorldSampleAgent::handleMessage(ICommunication::MessageRecord &record)
{
	char msgBuffer[256];
	char command[32];
	char arg1[32];
	char arg2[32];

	strcpy(arg1, "");
	strcpy(arg2, "");
	sscanf(record.msg, "%s %s %s", command, arg1, arg2);

	if((strcmp(command,"timeLeftInRound")==0) || (strcmp(command,"tR")==0))
	{   
		float timeLeft = getWorld().getTimeRoundEnds() - getWorld().getTime();
		sprintf(msgBuffer, "Time left in this round is %d:%d.\n", (int) timeLeft / 60, (int) timeLeft % 60);
		getCommunication().sayText(msgBuffer);
	}
	else if((strcmp(command,"mapType")==0) || (strcmp(command,"mT")==0))
	{   
		MapTypes::Enumeration mapType = getWorld().getMapType();
		if(mapType == MapTypes::bombDefuse)
			sprintf(msgBuffer, "This is a bomb defuse map.\n");
		else if(mapType == MapTypes::assassination)
			sprintf(msgBuffer, "This is an assassination map.\n");
		else if(mapType == MapTypes::hostageRescue)
			sprintf(msgBuffer, "This is a hostage rescue map.\n");
		getCommunication().sayText(msgBuffer);
	}
	else if((strcmp(command,"mapName")==0) || (strcmp(command,"mN")==0))
	{   
		char buffer[MAP_NAME_MAX_SIZE];
		getWorld().getMapName(buffer);
		sprintf(msgBuffer, "The map name is '%s'.\n", buffer);
		getCommunication().sayText(msgBuffer);
	}
	else if((strcmp(command,"isBombPlanted")==0) || (strcmp(command,"iBP")==0))
	{   
		if(getWorld().isBombPlanted())
			sprintf(msgBuffer, "Bomb was planted at %.2f.\n", getWorld().getTimeBombWasPlanted());
		else 
			sprintf(msgBuffer, "Bomb is not planted.\n");
		getCommunication().sayText(msgBuffer);
	}
	else if((strcmp(command,"toggleMonitorDamage")==0) || (strcmp(command,"tMD")==0))
	{   
		_isMonitoringDamage = !_isMonitoringDamage;

		if(_isMonitoringDamage)
			sprintf(msgBuffer, "I'm monitoring damage...\n");
		else 
			sprintf(msgBuffer, "I've stopped monitoring damage.\n");
		getCommunication().sayText(msgBuffer);
	}
	else if((strcmp(command,"health")==0) || (strcmp(command,"h")==0))
	{   
		sprintf(msgBuffer, "My health is: %d.\n", getHealth());
		getCommunication().sayText(msgBuffer);
	}
	else if(strcmp(command,"id")==0)
	{   
		sprintf(msgBuffer, "My name is '%s' and my id is %d.\n", getName(), getUniqueId());
		getCommunication().sayText(msgBuffer);
	}
	else if(strcmp(command, "Hello")==0)
	{
		sprintf(msgBuffer, "Hello World!");
		getCommunication().sayText(msgBuffer);
	}
}